package com.heatup.apt.management.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.heatup.apt.common.dao.SysRoleMapper;
import com.heatup.apt.common.dao.SysUserMapper;
import com.heatup.apt.common.util.MD5;
import com.heatup.apt.common.util.ResultBean;
import com.heatup.apt.common.util.StringUtil;
import com.heatup.apt.management.service.LoginService;
import com.heatup.apt.model.management.SysMenu;
import com.heatup.apt.model.management.SysRole;
import com.heatup.apt.model.management.SysUser;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class LoginServiceImpl implements LoginService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysRoleMapper sysRoleMapper;

    /**
     * 登录
     * <P>Author : fangxiaowen </P>      
     * <P>Date : 2016年9月20日 </P>
     * @param userName 用户名
     * @param userPwd 密码
     * @param request
     * @return
     * @see com.heatup.apt.management.service.LoginService#login(java.lang.String, java.lang.String, javax.servlet.http.HttpServletRequest)
     */
    @Override
    public ResultBean login(String userName, String userPwd,HttpServletRequest request) {
    	
    	userPwd = MD5.getMessageDigest(userPwd.toString().trim().getBytes()).toUpperCase();
    	
    	ResultBean resultBean = new ResultBean();
    	Map<String, String> params = new HashMap<String, String>();
    	params.clear();
    	params.put("userName", userName);
    	params.put("userPwd", userPwd);
    	SysUser sysUser = sysUserMapper.querySysUserByMap(params);
    	if (null == sysUser) {
    		resultBean.setCode(ResultBean.CODE_FAIL);
    		resultBean.setMsg("用户名或密码错误!");
    		return resultBean;
		}
    	SysRole sysRole = sysRoleMapper.querySysRoleById(sysUser.getRoleId());
    	
    	List<SysMenu> menus = sysUserMapper.queryUserMenuByUserId(sysUser.getId());

    	List<SysMenu> parentMenus = sysUserMapper.queryUserParentMenuByUserId(sysUser.getId());

    	List<SysMenu> subMenus = sysUserMapper.queryUserSubMenuByUserId(sysUser.getId());

    	
    	String menus_stri = "";
    	String path = request.getContextPath();
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

    	for(SysMenu sysMenu:parentMenus){
			if (StringUtil.isEquals(sysMenu.getParentNo(), 0)) {
				if (StringUtil.isEquals("1", sysMenu.getIsLeaf())) {
					menus_stri += "<li><a href='" + basePath + sysMenu.getMenuUrl() + "'><i class='"+sysMenu.getStyle()+"'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span></a></li>";
				}else{
					menus_stri += "<li>";
					menus_stri += "<a href='#'><i class='"+sysMenu.getStyle()+"'></i> <span class='nav-label'>"+sysMenu.getMenuName()+"</span><span class='fa arrow'></span></a>";
					menus_stri += "<ul class='nav nav-second-level collapse'>";

					for (SysMenu childSysMenu : subMenus) {
						if (StringUtil.isEquals(childSysMenu.getParentNo(), sysMenu.getId())) {
							menus_stri += "<li class='active'><a href='"+basePath+childSysMenu.getMenuUrl()+"'>"+childSysMenu.getMenuName()+"</a></li>";
						}
					}
					menus_stri += "</ul>";
					menus_stri += "</li>";
				}
			}
		}


		/**
    	for (SysMenu sysMenu : menus) {
    		
    		
				if (StringUtil.isEquals(sysMenu.getParentNo(), 0)) {
					
					//如果是根跳转 is_leaf = 1
		    		if (StringUtil.isEquals("1", sysMenu.getIsLeaf())) {
		    		    if(sysMenu.getId().intValue() ==1) {
                            //1 运营位管理
                            menus_stri += "<li><a href='" + basePath + sysMenu.getMenuUrl() + "'><i class='fa fa-th-large'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span></a></li>";
                        }else if(sysMenu.getId().intValue() == 4){
		    		        //4 订单管理
                            menus_stri += "<li><a href='" + basePath + sysMenu.getMenuUrl() + "'><i class='fa fa-pie-chart'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span></a></li>";
                        }else if(sysMenu.getId().intValue() ==11){
                            //11 数据中心
                            menus_stri += "<li><a href='" + basePath + sysMenu.getMenuUrl() + "'><i class='fa fa-bar-chart-o'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span></a></li>";
                        } else {
						    menus_stri += "<li><a href='"+basePath + sysMenu.getMenuUrl()+"'><i class='fa fa-th-large'></i> <span class='nav-label'>"+sysMenu.getMenuName()+"</span></a></li>";
                        }
					}else{
						menus_stri += "<li>";
						if(sysMenu.getId().intValue() == 2){
							//2 课程管理
							menus_stri += "<a href='#'><i class='fa fa-table'></i> <span class='nav-label'>"+sysMenu.getMenuName()+"</span><span class='fa arrow'></span></a>";
						}else if(sysMenu.getId().intValue() ==15){
							//15 教练管理
							menus_stri += "<a href='#'><i class='fa fa-diamond'></i> <span class='nav-label'>"+sysMenu.getMenuName()+"</span><span class='fa arrow'></span></a>";
						}else if(sysMenu.getId().intValue() == 7){
							//7 门店管理
							menus_stri += "<a href='#'><i class='fa fa-sitemap'></i> <span class='nav-label'>"+sysMenu.getMenuName()+"</span><span class='fa arrow'></span></a>";
						}else if(sysMenu.getId().intValue() == 8) {
                            //8 权限管理
                            menus_stri += "<a href='#'><i class='fa fa-laptop'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span><span class='fa arrow'></span></a>";
                        }else if(sysMenu.getId().intValue() == 21) {
                            //21 客户管理
                            menus_stri += "<a href='#'><i class='fa fa-wechat'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span><span class='fa arrow'></span></a>";
						}else if(sysMenu.getId().intValue() == 26) {
							//26 优惠券管理
							menus_stri += "<a href='#'><i class='fa fa-tags'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span><span class='fa arrow'></span></a>";
						}else if(sysMenu.getId().intValue() == 35) {
							//35 储值卡管理
							menus_stri += "<a href='#'><i class='fa fa-credit-card'></i> <span class='nav-label'>" + sysMenu.getMenuName() + "</span><span class='fa arrow'></span></a>";
						}else {
							menus_stri += "<a href='#'><i class='fa fa-database'></i> <span class='nav-label'>"+sysMenu.getMenuName()+"</span><span class='fa arrow'></span></a>";
						}

						menus_stri += "<ul class='nav nav-second-level collapse'>";
						for (SysMenu childSysMenu : menus) {
							if (StringUtil.isEquals(childSysMenu.getParentNo(), sysMenu.getId())) {
								menus_stri += "<li class='active'><a href='"+basePath+childSysMenu.getMenuUrl()+"'>"+childSysMenu.getMenuName()+"</a></li>";
							}
						}
						menus_stri += "</ul>";
						menus_stri += "</li>";
					}
				}
		}**/
    	
    	
    	HttpSession session = request.getSession();
    	session.setAttribute("sys_user", sysUser);
    	session.setAttribute("sys_role", sysRole);
    	session.setAttribute("menus", menus_stri);
    	
    	resultBean.setCode(ResultBean.CODE_SUCCESS);
		resultBean.setMsg("登录成功!");
		
        return resultBean;
    }
    
    public static void main(String[] args) {
    	System.out.println(MD5.getMessageDigest("123456".toString().trim().getBytes()).toUpperCase());
    	
	}
}
